package com.example.com.jqx.testlistview;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity implements OnScrollListener {
	private ListView lv;
	private TextView testtv;
	protected View currentView;
	private int screenHeight;
	private int screenWidth;
	protected int currentPosition;
	private boolean isVisisble;
	public int currentPos = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		screenHeight = ScreenUtils.getScreenHeight(this);
		screenWidth = ScreenUtils.getScreenWidth(this);
		lv = (ListView) findViewById(R.id.testlv);
		testtv = (TextView) findViewById(R.id.testtv);
		Adapter adapter = new Adapter(this);
		lv.setAdapter(adapter);
		lv.setOnScrollListener(this);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				currentView = arg1;
				currentPosition = arg2;
				currentPos = arg2;
				testtv.setVisibility(View.VISIBLE);

				int[] location = new int[2];
				currentView.getLocationInWindow(location);

				Rect frame = new Rect();
				getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
				int statusBarHeight = frame.top;
				System.out.println("  statusBarHeight: " + statusBarHeight);
				int x = location[0];
				int y = location[1];
				System.out.println("    touch--    x: " + x + "  y:" + y);

				int dY = y - statusBarHeight;
				RelativeLayout.LayoutParams lp = (LayoutParams) testtv
						.getLayoutParams();
				lp.width = screenWidth;
				lp.height = 400;
				lp.setMargins(0, dY, 0, 0);

				testtv.setLayoutParams(lp);
			}
		});
	}

	@Override
	protected void onStart() {

		super.onStart();
	}

	class Adapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public Adapter(Context context) {
			super();
			// TODO Auto-generated constructor stub
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 10;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.test_item, null);
			}

			TextView tv = (TextView) convertView.findViewById(R.id.tv);
			tv.setText(position + "");

			// if (currentPos != -1) {
			// if (currentPos <= position) {
			// currentView = convertView;
			// } else {
			// currentView = null;
			// }
			// }

			// if (convertView==currentView) {
			//
			// }

			return convertView;
		}

	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		System.out.println("onScroll current position:" + currentPosition + ","
				+ firstVisibleItem);
		if (currentView == null) {
			RelativeLayout.LayoutParams lp = (LayoutParams) testtv
					.getLayoutParams();
			lp.width = screenWidth / 3;
			lp.height = 400 / 3;
			lp.setMargins(screenWidth - lp.width, screenHeight - lp.height
					- 200, 0, 0);
			testtv.setLayoutParams(lp);
			return;
		}

		// if (firstVisibleItem == currentPosition) {
		// currentView = lv.getChildAt(currentPosition);
		// }

		testtv.setVisibility(View.VISIBLE);

		int[] location = new int[2];
		currentView.getLocationInWindow(location);

		Rect frame = new Rect();
		getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;
		System.out.println("  statusBarHeight: " + statusBarHeight);
		int x = location[0];
		int y = location[1];
		System.out.println("    touch--    x: " + x + "  y:" + y
				+ ",visibleItemCount:" + visibleItemCount);

		int dY = y - statusBarHeight;
		RelativeLayout.LayoutParams lp = (LayoutParams) testtv
				.getLayoutParams();
		// if (dY < -200 || dY > (screenHeight - 200)) {
		// if (firstVisibleItem != currentPosition) {
		// lp.width = screenWidth / 3;
		// lp.height = 400 / 3;
		// lp.setMargins(screenWidth - lp.width, screenHeight - lp.height
		// - 200, 0, 0);
		// } else {
		// currentView = lv.getChildAt(currentPosition);
		// x = location[0];
		// y = location[1];
		// System.out.println("    touch1--    x: " + x + "  y:" + y);
		//
		// dY = y - statusBarHeight;
		lp.width = screenWidth;
		lp.height = 400;
		lp.setMargins(0, dY, 0, 0);
		// }

		testtv.setLayoutParams(lp);
	}
}
